#Based on Pylons middleware.py

from paste import httpexceptions
from paste.cascade import Cascade
from paste.urlparser import StaticURLParser, make_static
from paste.registry import RegistryManager
from paste.deploy.config import ConfigMiddleware
from paste.recursive import RecursiveMiddleware
from paste.urlmap import URLMap
from paste.translogger import TransLogger

from beaker.cache import CacheMiddleware

#global_conf is a dict representing the Paste configuration options,
#paste.deploy.converters should be used when parsing Paste config options
#to ensure they're treated properly.

#bccore is defined by the caller
app = bccore

# @@@ Expose config variables to other plugins @@@
app = ConfigMiddleware(app, {'app_conf':app_conf,
        'global_conf':global_conf})

# Main place for plug-ins
# Put your own middleware here, so that any problems are caught by the error
# handling middleware below

#Sample of user-specified midleware
#Support server-side processing of XML/XSLT specified in the app response   
#from wsgixml.applyxslt import applyxslt
#app = applyxslt(app, use_wsgi_env=True, stock_xslt_params=None, ext_modules=['ftgoodies.extmods.markdown'])

# @@@ Caching support from Beaker @@@
app = CacheMiddleware(app, global_conf)

# @@@ Change HTTPExceptions to HTTP responses @@@
app = httpexceptions.make_middleware(app, global_conf)
    
# @@@ Error Handling @@@
#app = ErrorHandler(app, global_conf, error_template=error_template, **config.errorware)
    
# @@@ Serve static files from the specified directory @@@
static_app = StaticURLParser(app_conf['static_files'])
#static = make_static(global_conf, 'brightcontent/static')

from brightcontent.plugins.atomprotocol import WsgiAtomPublishingProtocol
atomprotocol = WsgiAtomPublishingProtocol(None, store, app_conf)
 
urlmap = URLMap()
urlmap['/blog'] = app
urlmap['/static'] = static_app
urlmap['/atom'] = atomprotocol

# @@@ Permits recursive calls into the middleware stack, to save overhead for internal and forwarded requests @@@
app = RecursiveMiddleware(urlmap, global_conf)
     
app = TransLogger(app)
   
#static_app = StaticURLParser(config.paths['static_files'])
